import {
	request2
} from "../../utils/request2.js";

import {
	request
} from "../../utils/request.js";
wx - Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		//轮播图数据
		swiperList: [],
		//表单数据
		catesList: [],

		flag: false,
		// code:""
		scanCode: {},
		//商品数据
		infoList: {},
		//导航名称
		name: "",
		baseSrc: "",
		hasSwiper: false,
		hasCates: false

		// oldId:"",
		// oldTime:""

	},

 /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let persInfo = wx.getStorageSync('persInfo');
    var name = persInfo.name;
    
    // 页面显示时刷新
    const pages = getCurrentPages() 
    const perpage = pages[pages.length - 1]
    perpage.onLoad(this.options)  

    // 查看当前用户是否有新的消息
      request2({ 
        url: "/servlet/message/anynew",
        method: "GET",
        data: {
          name: name 
        } 
      })
      .then(result => { 
        var anynew = result.data.data;
        console.log(anynew)
        if(anynew == true){
          wx.showTabBarRedDot({
            index: 1,
          }); 
        }
        this.setData({ 
        })
      })
      .catch(err => {})  
  },

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
    // console.log(options);  
		this.getSwiperList();
		this.getCatesList(options.roleId);
		this.getBaseSrc();
	},
	getBaseSrc() {
				const surl = getApp().globalData.serverUrl+"servlet/web/util/ShowPicture?file=";
		this.setData({
			baseSrc: surl
		})
	},
	getSwiperList() {
		request({				
				url: '/wx/SwipperServlet'
			})
			.then((result) => {

				this.setData({
					swiperList: result.data.message,
					hasSwiper: true
				})
			}).catch(err => {

				console.log("index页面的网络故障或者服务器崩溃!");
			})

	},
	getCatesList(roleId) {
		const sid = wx.getStorageSync('sid')
		console.log(sid);

		request2({

				url: "/servlet/service/list4role",
				data: {
					sid: sid,
					roleNo:roleId
				}
			})
			.then(result => {
				console.log(result);
				this.setData({
					catesList: result.data.rows,
					hasCates: true
				})
			})
			.catch(err => {

			})

	},
	async getCateInfo(e) {		

		const personInfo = wx.getStorageSync('persInfo');
		try {
			const res = await request2({
				
				url: "/servlet/service/list4role",
				data: {
					roleNo:personInfo.roleId,
					sid: personInfo.sid
				}
			});
		
			const {
				index
			} = e.currentTarget.dataset;
			// console.log(index);
			var name = res.data.message[index].name;
			//  console.log(name);
			this.setData({
				name: name,
				hasCates: true
			})
		} catch (err) {

		}
	},
	
})